package mdt.action;

import org.tigris.gef.base.ModePlace;
import org.tigris.gef.graph.GraphFactory;
import org.tigris.gef.persistence.pgml.PgmlUtility;
import org.tigris.gef.presentation.Fig;
import mdt.figure.FigNetworkDevice;

import java.awt.event.MouseEvent;
import java.util.List;

/**
 * Created by lambert on 2016/6/21.
 */
public class ModePlaceNetworkDevice extends ModePlace {
	public ModePlaceNetworkDevice(GraphFactory gf) {
		super(gf);
	}

	public ModePlaceNetworkDevice(GraphFactory gf, String instructions) {
		super(gf, instructions);
	}



	@Override
	public void mousePressed(MouseEvent me) {
		super.mousePressed(me);
		postProcessing();
	}

	@Override
	public void mouseDragged(MouseEvent me) {
		super.mouseDragged(me);
		postProcessing();
	}


	/**
	 * Set height and Y position of the new CR as the already existing CRs.
	 */
	private void postProcessing() {
		List nodes =
				PgmlUtility.getContentsNoEdges(editor.getLayerManager().getActiveLayer());
		int i = 0;
		boolean figNetworkDeviceFound = false;
		Fig fig = null;

		// Get the first existing FigNode and if it exists set the
		// y position and height of _pers to be the same as it.
		while (i < nodes.size() && !figNetworkDeviceFound) {
			fig = (Fig) nodes.get(i);
			if (fig != _pers && fig instanceof FigNetworkDevice) {
				_pers.setY(fig.getY());
				_pers.setHeight(fig.getHeight());
				figNetworkDeviceFound = true;
			}
			i++;
		}
	}
}
